<template>
  <div class="mobile-page" :style="boxStyle" v-if="configObj">
    <div class="enroll-container">
      <!-- 报名图片 -->
      <div class="enroll-item">
        <div class="item-title">报名图片</div>
        <div class="img-container">
          <el-image
            class="enroll-img"
            :src="configObj.enrollImage.img"
            alt="报名图片"
            v-if="configObj.enrollImage.img"
            fit="contain"
          />
          <div class="empty-box" v-else>
            <span class="iconfont iconfont icontupian"></span>
          </div>
        </div>
      </div>

      <!-- 创意图片 -->
      <div class="enroll-item">
        <div class="item-title">创意图片</div>
        <div class="img-container">
          <img
            class="enroll-img"
            :src="configObj.creativeImage.img"
            alt="创意图片"
            v-if="configObj.creativeImage.img"
          />
          <div class="empty-box" v-else>
            <span class="iconfont iconfont icontupian"></span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex';
export default {
  name: 'home_enroll', // 组件名称
  cname: '报名图片', // 标题名称
  icon: 't-icon-zujian-tupian',
  defaultName: 'enrollImage', // 外面匹配名称
  configName: 'c_home_enroll', // 右侧配置名称
  type: 0, // 0 基础组件 1 营销组件 2工具组件
  props: {
    index: { type: null },
    num: { type: null },
  },
  data() {
    return {
      // 默认初始化数据禁止修改
      defaultConfig: {
        name: 'enrollImage',
        isShow: true,
        isHide: false,
        timestamp: this.num,
        setUp: {
          tabVal: 0,
          cname: '报名图片',
        },
        // 报名图片
        enrollImage: {
          tabTitle: '图片设置',
          title: '报名图片',
          tips: '建议：图片尺寸750*332px',
          img: '',
          info: [
            {
              title: '链接',
              value: '',
              tips: '请选择链接',
              max: 100,
            },
          ],
        },
        // 创意图片
        creativeImage: {
          title: '创意图片',
          tips: '建议：图片尺寸750*332px',
          img: '',
          info: [
            {
              title: '链接',
              value: '',
              tips: '请选择链接',
              max: 100,
            },
          ],
        },
        // 上间距
        upConfig: {
          title: '上边距',
          val: 10,
          min: 0,
          max: 100,
        },
        // 下间距
        downConfig: {
          tabTitle: '边距设置',
          title: '下边距',
          val: 10,
          min: 0,
        },
        bgStyle: {
          tabTitle: '圆角设置',
          title: '背景圆角',
          name: 'bgStyle',
          val: 0,
          min: 0,
          max: 30,
        },
        // 左右间距
        lrConfig: {
          title: '左右边距',
          val: 12,
          min: 0,
          max: 15,
        },
        // 页面间距
        mbConfig: {
          title: '页面间距',
          val: 0,
          min: 0,
        },
        // 背景颜色
        bgColor: {
          tabTitle: '颜色设置',
          title: '背景颜色',
          color: [
            {
              item: '#FFFFFF',
            },
          ],
          default: [
            {
              item: '#FFFFFF',
            },
          ],
        },
      },
      configObj: null,
    };
  },
  computed: {
    ...mapState('mobildConfig', ['defaultArray']),
    //最外层盒子的样式
    boxStyle() {
      return [
        { 'border-radius': this.configObj.bgStyle.val ? this.configObj.bgStyle.val + 'px' : '0' },
        { background: this.configObj.bgColor.color[0].item },
        { margin: this.configObj.mbConfig.val + 'px' + ' ' + this.configObj.lrConfig.val + 'px' + ' ' + 0 },
        { padding: this.configObj.upConfig.val + 'px' + ' ' + 0 + ' ' + this.configObj.downConfig.val + 'px' },
      ];
    },
    // 监听store中对应索引的数据变化
    storeData() {
      console.log(111, this.num);
      return this.$store.state.mobildConfig.defaultArray[this.num];
    },
  },
  watch: {
    defaultArray: {
      handler(nVal, oVal) {
        let data = this.$store.state.mobildConfig.defaultArray[this.num];
        this.setConfig(data);
      },
      deep: true,
    },
    storeData: {
      handler(newVal) {
        if (newVal) {
          console.log('newVal', newVal);

          this.setConfig(newVal);
        }
      },
      deep: true,
    },
  },
  mounted() {
    this.$nextTick(() => {
      if (this.num) {
        let data = this.$store.state.mobildConfig.defaultArray[this.num];
        this.setConfig(data);
      }
    });
  },
  methods: {
    onChange() {},
    setConfig(data) {
      if (data) {
        this.configObj = data;
      }
    },
  },
};
</script>
<style scoped lang="scss">
.mobile-page {
  position: relative;
  width: auto;
}

.enroll-container {
  padding: 15px;
}

.enroll-item {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.item-title {
  font-size: 14px;
  color: #333;
  margin-bottom: 10px;
  font-weight: 500;
}

.img-container {
  position: relative;
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
}

.enroll-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.empty-box {
  width: 100%;
  height: 100%;
  background: #f3f9ff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icontupian {
  font-size: 30px !important;
  color: #bbbfc8;
}
</style>
